<?php
namespace Poi\EttsBundle\Model;

/**
 * @author Ricardo J. Vazquez
 * 
 * Collection of Officer models
 */
class Officers extends Collection
{
	public function init(){	
	}
	
	/**
	* Loads the collection with Officer models
	*
	* @param $col 
	*/
	public function loadOfficers(Collection $col){
		$sql = "SELECT
					O1.BadgeNumber,
					O1.Name,
					O1.PoliceUnit
				FROM Officer O1";
				
		$loadStmt = $this->conn->prepare($sql);

		$loadStmt->execute();

		$officers = $loadStmt->getWrappedStatement()->fetchAll(
			\PDO::FETCH_CLASS, 
			"Poi\\EttsBundle\\Model\\Officer", 
			array($this->conn)
		);

		$count = 0;
		foreach($officers as $officer){
			$officer->setLoaded(true);
			$this->addItem($officer, $count++);
		}
	}
	
	public function toXML(\SimpleXMLElement &$xml=null){
		$return = false;
		if(is_null($xml)){
			$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?><xmlresponse></xmlresponse>');
			$return = true;
		}
		
		$length = $this->length();
		
		$officersXML = $xml->addChild('officers');
		
		for($i = 0; $i < $length; $i++)
		{
			$this->getItem($i)->toXML($officersXML);
		}
				
		if($return){		
			return $xml->asXML();
		}
	}
	
}